//
// Created by yangchao on 2022/6/6.
// 128. 最长连续序列: https://leetcode.cn/problems/longest-consecutive-sequence/
//

#include <vector>
#include <unordered_set>

using namespace std;

//注意审题，是连续序列：1,2,3，4......
class LongestConsecutive {
public:
    int longestConsecutive(vector<int>& nums) {
        unordered_set<int> num_set;
        for (const int& num : nums) {
            num_set.insert(num);
        }
        int longestStreak = 0;
        for (const int& num: num_set) {
            if (!num_set.count(num-1)) {
                int currentNum = num;
                int currentStreak = 1;
                while (num_set.count(currentNum+1)) {
                    currentNum += 1;
                    currentStreak += 1;
                }
                longestStreak = max(longestStreak,currentStreak);
            }
        }
        return longestStreak;
    }
};

